package ${basePackage}.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import ${basePackage}.${entityPackage}.${className};
import ${basePackage}.${servicePackage}.I${className}Service;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * ${tableComment} Controller
 *
 * @author ${author}
 * @date ${date}
 */
@Slf4j
@Validated
@Controller
public class ${className}Controller extends BaseController {

    @Autowired
    private I${className}Service ${className?uncap_first}Service;

    @GetMapping(FebsConstant.VIEW_PREFIX + "${className?uncap_first}")
    @RequiresPermissions("${className?uncap_first}:view")
    public String ${className?uncap_first}Index(){
        return jumpPage("${mapperXmlPackage}/${className?uncap_first}/${className?uncap_first}List");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "${className?uncap_first}/add")
    @RequiresPermissions("${className?uncap_first}:add")
    public String ${className?uncap_first}Add(){
        return jumpPage("${mapperXmlPackage}/${className?uncap_first}/${className?uncap_first}Edit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "${className?uncap_first}/update/{id}")
    @RequiresPermissions("${className?uncap_first}:edit")
    public String ${className?uncap_first}Update(Model model, @PathVariable("id") String id){
        model.addAttribute("${className?uncap_first}", ${className?uncap_first}Service.find${className}ById(id));
        return jumpPage("${mapperXmlPackage}/${className?uncap_first}/${className?uncap_first}Edit");
    }

    /**
     * 查询（所有）
     * @author ${author}
     * @date ${date}
     */
    @GetMapping("${className?uncap_first}")
    @ResponseBody
    @RequiresPermissions("${className?uncap_first}:view")
    public FebsResponse getAll${className}s(${className} ${className?uncap_first}) {
        try {
            return success(${className?uncap_first}Service.find${className}List(${className?uncap_first}));
        }catch (Exception ex){
            return failure(ex);
        }

    }

    /**
     * 查询（分页）
     * @author ${author}
     * @date ${date}
     */
    @GetMapping("${className?uncap_first}/list")
    @ResponseBody
    @RequiresPermissions("${className?uncap_first}:view")
    public FebsResponse ${className?uncap_first}List(QueryRequest request, ${className} ${className?uncap_first}) {
        try {
            Map<String, Object> dataTable = getDataTable(this.${className?uncap_first}Service.find${className}Page(request, ${className?uncap_first}));
            return success(dataTable);
        }catch (Exception ex){
            return failure(ex);
        }
    }

    /**
     * 新增 ${className}
     * @author ${author}
     * @date ${date}
     */
    @Log("新增${className}")
    @PostMapping("${className?uncap_first}")
    @ResponseBody
    @RequiresPermissions("${className?uncap_first}:add")
    public FebsResponse add${className}(@Valid ${className} ${className?uncap_first}) throws FebsException {
        try {
            this.${className?uncap_first}Service.create${className}(${className?uncap_first});
            return success();
        } catch (Exception ex) {
            return failure(ex,"新增失败");
        }
    }

    /**
     * 逻辑删除 ${className}
     * @author ${author}
     * @date ${date}
     */
    @Log("逻辑删除${className}")
    @GetMapping("${className?uncap_first}/delete/{id}")
    @ResponseBody
    @RequiresPermissions("${className?uncap_first}:delete")
    public FebsResponse delete${className}(@PathVariable("id") String ${className?uncap_first}Ids) throws FebsException {
        try {
            ${className} ${className?uncap_first} = new ${className}();
            ${className?uncap_first}.setDeletedBy(getCurrentUser().getUserName());
            ${className?uncap_first}.setDeletedTime(new Date());
            ${className?uncap_first}.setDeletedFlg(FebsConstant.INVALID);
            this.${className?uncap_first}Service.delete${className}(${className?uncap_first},${className?uncap_first}Ids);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
    * 物理删除 ${className}
    * @author ${author}
    * @date ${date}
    */
    @Log("物理删除${className}")
    @GetMapping("${className?uncap_first}/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("${className?uncap_first}:delete")
    public FebsResponse physicallyDelete${className}(@PathVariable("id") String ${className?uncap_first}Ids) throws FebsException {
        try {
            this.${className?uncap_first}Service.physicallyDelete${className}(${className?uncap_first}Ids);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
     * 修改 ${className}
     * @author ${author}
     * @date ${date}
     */
    @Log("修改${className}")
    @PostMapping("${className?uncap_first}/update")
    @ResponseBody
    @RequiresPermissions("${className?uncap_first}:edit")
    public FebsResponse update${className}(${className} ${className?uncap_first}) throws FebsException {
        try {
            this.${className?uncap_first}Service.update${className}(${className?uncap_first});
            return success();
        } catch (Exception ex) {
            return failure(ex,"修改失败");
        }
    }

}
